package com.project.platform.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.project.platform.dto.CurrentUserDTO;
import com.project.platform.dto.RetrievePasswordDTO;
import com.project.platform.dto.UpdatePasswordDTO;
import com.project.platform.entity.Family;
import com.project.platform.entity.Staff;
import com.project.platform.exception.CustomException;
import com.project.platform.mapper.StaffMapper;
import com.project.platform.service.StaffService;
import com.project.platform.utils.CurrentUserThreadLocal;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.project.platform.vo.PageVO;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * ${table.name}
 */
@Service
public class StaffServiceImpl implements StaffService {
    @Resource
    private StaffMapper staffMapper;

    @Value("${resetPassword}")
    private String resetPassword;

    @Override
    public PageVO<Staff> page(Map<String, Object> query, Integer pageNum, Integer pageSize) {
        PageVO<Staff> page = new PageVO();
        List<Staff> list = staffMapper.queryPage((pageNum - 1) * pageSize, pageSize, query);
        page.setList(list);
        page.setTotal(staffMapper.queryCount(query));
        return page;
    }

    @Override
    public CurrentUserDTO login(String username, String password) {
        Staff staff = staffMapper.selectByUserName(username);
        if (staff == null || !staff.getPassword().equals(password)) {
            throw new CustomException("用户名或密码错误");
        }
        if (staff.getStatus().equals("禁用")) {
            throw new CustomException("用户已禁用");
        }
        CurrentUserDTO currentUserDTO = new CurrentUserDTO();
        BeanUtils.copyProperties(staff, currentUserDTO);
        return currentUserDTO;
    }

    @Override
        public void register(JSONObject data) {
            Staff staff = new Staff();
            staff.setUsername(data.getString("username"));
            staff.setNickname(data.getString("nickname"));
            staff.setAvatarUrl(data.getString("avatarUrl"));
            staff.setPassword(data.getString("password"));
            staff.setStatus("启用");
            insert(staff);
        }

    @Override
    public void updateCurrentUserInfo(CurrentUserDTO currentUserDTO) {
        Staff staff = staffMapper.selectById(currentUserDTO.getId());
        staff.setNickname(currentUserDTO.getNickname());
        staff.setAvatarUrl(currentUserDTO.getAvatarUrl());
        staff.setTel(currentUserDTO.getTel());
        staff.setEmail(currentUserDTO.getEmail());
        staffMapper.updateById(staff);
    }

    @Override
    public void updateCurrentUserPassword(UpdatePasswordDTO updatePassword) {
        Staff staff = staffMapper.selectById(CurrentUserThreadLocal.getCurrentUser().getId());
        if (!staff.getPassword().equals(updatePassword.getOldPassword())) {
            throw new CustomException("旧密码不正确");
        }
        staff.setPassword(updatePassword.getNewPassword());
        staffMapper.updateById(staff);
    }

    @Override
    public void resetPassword(Integer id) {
        Staff staff = staffMapper.selectById(id);
        staff.setPassword(resetPassword);
        staffMapper.updateById(staff);
    }

    @Override
    public void retrievePassword(RetrievePasswordDTO retrievePasswordDTO) {
        Staff staff = staffMapper.selectByTel(retrievePasswordDTO.getTel());
        if (staff == null) {
            throw new CustomException("手机号不存在");
        }
        staff.setPassword(retrievePasswordDTO.getPassword());
        staffMapper.updateById(staff);
    }

    @Override
    public Staff selectById(Integer id) {
        Staff staff = staffMapper.selectById(id);
        return staff;
    }

    @Override
    public List<Staff> list() {
        return staffMapper.list();
    }

    @Override
    public void insert(Staff entity) {
        check(entity);
        staffMapper.insert(entity);
    }

    @Override
    public void updateById(Staff entity) {
        check(entity);
        staffMapper.updateById(entity);
    }

    private void check(Staff entity) {
        Staff staff = staffMapper.selectByUserName(entity.getUsername());
        if (staff != null && !Objects.equals(staff.getId(), entity.getId())) {
            throw new CustomException("用户名已存在");
        }
    }

    @Override
    public void removeByIds(List<Integer> ids) {
        staffMapper.removeByIds(ids);
    }
}
